package top.zlhy7.module.dept.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.zlhy7.base.response.ResultVO;
import top.zlhy7.module.dept.service.ISysDeptService;
import top.zlhy7.module.dept.vo.SysDeptVo;

/**
 * @author renyong
 * @date 2024/9/29 19:38
 * @description 部门表
 */
@RequiredArgsConstructor
@Slf4j
@RestController
@RequestMapping("sysDept")
public class SysDeptController {
    /**
     * 部门
     */
    private final ISysDeptService sysDeptService;

    /**
     * 详情
     * @param deptId 主键id
     * @return
     * @author renyong on 2024/9/29 22:40
     */
    @GetMapping("{deptId:\\d+}")
    public ResultVO detail(@PathVariable Long deptId){
        return ResultVO.ok(sysDeptService.getById(deptId));
    }
    /**
     * 查询一级部门
     * @param parentId 父级id
     * @return
     * @author renyong on 2024/9/30 00:09
     */
    @GetMapping("parentId/{parentId:\\d+}")
    public ResultVO findAllByParentId(@PathVariable Long parentId){
        return ResultVO.ok(sysDeptService.findAllByParentId(parentId));
    }
    /**
     * 查询所有数据树结构
     * @param
     * @return
     * @author renyong on 2024/9/30 00:08
     */
    @GetMapping("allTree")
    public ResultVO findTree(){
        return ResultVO.ok(sysDeptService.findTree());
    }
    /**
     * 新增
     * @param sysDeptVo 新增参数
     * @return
     * @author renyong on 2024/9/29 22:02
     */
    @PostMapping("save")
    public ResultVO insert(@RequestBody @Validated SysDeptVo sysDeptVo){
        sysDeptService.insert(sysDeptVo);
        return ResultVO.ok();
    }
    /**
     * 修改
     * @param sysDeptVo 修改参数
     * @return
     * @author renyong on 2024/9/29 22:02
     */
    @PostMapping("update")
    public ResultVO update(@RequestBody @Validated SysDeptVo sysDeptVo){
        sysDeptService.update(sysDeptVo);
        return ResultVO.ok();
    }
    /**
     * 删除
     * @param id 主键id
     * @return
     * @author renyong on 2024/9/29 22:02
     */
    @PostMapping("delete")
    public ResultVO delete(Long id){
        sysDeptService.delete(id);
        return ResultVO.ok();
    }
}
